<?php
namespace Common\Model;

use Think\Model\RelationModel;


class CommentModel extends RelationModel
{
    protected $_link = array(
        'User' => array(
            'mapping_type' => self::BELONGS_TO,
            'mapping_name' => 'user',
            'foreign_key' => 'user_id',//关联id
            'as_fields' => 'username:username',
        ),
        'Product' => array(
            'mapping_type' => self::BELONGS_TO,
            'mapping_name' => 'product',
            'foreign_key' => 'product_id',//关联id
            'as_fields' => 'name:product_name',
        ),
    );

    public function getComment($condition = array(), $relation = false)
    {
        $data = $this->where($condition);
        if ($relation) {
            $data = $data->relation(true);
        }
        $data = $data->find();

        return $data;
    }

    public function getCommentList($condition = array(), $relation = false, $order = "id desc", $p = 0, $num = 0, $limit = 0)
    {
        $list = $this->where($condition);
        if ($relation) {
            $list = $list->relation(true);
        }
        if ($p && $num) {
            $list = $list->page($p . ',' . $num . '');
        }
        if ($limit) {
            $list = $list->limit($limit);
        }

        $list = $list->order($order)->select();

        return $list;
    }

    public function getCommentListCount($condition = array())
    {
        $count = $this->where($condition)->count();
        return $count;
    }

    public function delComment($ids)
    {
        $this->where(array("id" => array("in", $ids)))->delete();
    }

    public function addAllComment($data)
    {
        $this->addAll($data);
    }
}